package com.example.myapplication;

import java.util.Stack;

public class ChessPiece extends ChessBaseItem {
    private Position curr_pos;
    private String id;
    private Stack<Position> stepStack;

    public ChessPiece(int posx, int posy, int width, int height, String id) {
        super(width, height);
        this.curr_pos = new Position(posx, posy);
        this.id = id;
        this.stepStack = new Stack();
    }

    public boolean move(Position new_pos) {
        this.stepStack.push(this.curr_pos);
        this.curr_pos = new_pos;
        return this.setLayout();
    }

    public Position undo() {
        this.curr_pos = (Position)this.stepStack.pop();
        return this.curr_pos;
    }

    public Position getPosition() {
        return this.curr_pos;
    }

    public Position getPrevPosition() {
        return !this.stepStack.empty() ? (Position)this.stepStack.lastElement() : null;
    }

    public String getId() {
        return this.id;
    }

    public void draw() {
        System.out.println(this.toString());
    }

    public boolean setLayout() {
        return this.curr_pos.getPosx() >= 0 && this.curr_pos.getPosy() >= 0;
    }

    public String toString() {
        return String.format("Piece id:%s, posx:%d, posy:%d, width:%d, height:%d", this.id, this.curr_pos.getPosx(), this.curr_pos.getPosy(), this.width, this.height);
    }
}